<?php

namespace app\controller;

use app\BaseController;
use app\model\UploadFile;
use app\model\TowerCrane;
use app\model\Softwareurl;
use app\model\WorkOrder;
use think\api\Client;
// use think\facade\Cache;
use think\facade\Db;
use think\facade\Filesystem;
use think\Request;

//use GuzzleHttp\Client;
//use app\middleware\Auth;

class Upload extends BaseController
{
    public function upload(Request $request)
    {
        $upload_url = env('UPLOAD_URL', true);
        $post = $request->post();
        $file = request()->file('file');
        //$文件名
        $fileName = $file->getOriginalName();
        $date = date('Y_m_d', time()); //当天日期
        $fileOssName = time() . $fileName; //阿里云文件名
        $ext = $file->getOriginalExtension(); //文件扩展名
        // $ossurl = config('filesystem.disks.aliyun.url');
        // 上传本地服务器
        $res = Filesystem::disk('public')->putFileAs($ext . '/' . $date, $file, $fileOssName); //按照文件类型分类
        $res = stripslashes($res); //去除转义字符
        $fileurl = 'https://' . $upload_url . '/' . $res;
        //地址存入数据库
        $upload = new UploadFile;
        $upload->url = $fileurl;
        $upload->filename = $fileOssName;
        if (isset($post['type'])) {
            $upload->type = $post['type'];
        }
        $upload->save();

        return success($upload);
    }
    public function uploadApp(Request $request)
    {
        $upload_url = env('UPLOAD_URL', true);
        $post = $request->post();
        $file = request()->file('file');
        //$文件名
        $fileName = $file->getOriginalName();
        $fileOssName = $fileName; //阿里云文件名
        // $ossurl = config('filesystem.disks.aliyun.url');
        // 上传本地服务器
        $res = Filesystem::disk('public')->putFileAs('app' . '/' . time(), $file, $fileOssName); //按照文件类型分类
        $res = stripslashes($res); //去除转义字符
        $fileurl = 'http://' . $upload_url . '/' . $res;
        //地址存入数据库
        $upload = new UploadFile;
        $upload->url = $fileurl;
        $upload->filename = $fileOssName;
        $upload->save();

        return success($upload);
    }
    public function uploadInstall(Request $request)
    {
        $upload_url = env('UPLOAD_URL', true);
        $file = request()->file('file');
        $post = $request->post();
        //$文件名
        $fileName = $file->getOriginalName();
        $date = date('Y_m_d', time()); //当天日期
        $fileOssName = time() . $fileName; //阿里云文件名
        $ext = $file->getOriginalExtension(); //文件扩展名
        // $ossurl = config('filesystem.disks.aliyun.url');
        // 上传本地服务器
        $res = Filesystem::disk('public')->putFileAs('equipmentInstall/' . $date, $file, $fileOssName); //按照文件类型分类
        $res = stripslashes($res); //去除转义字符
        $fileurl = $upload_url . '/' . $res;
        //地址存入数据库
        $upload = new WorkOrder;
        $upload->url = 'http://' . $fileurl;
        $upload->filename = $fileOssName;
        if (isset($post['eid'])) {
            $upload->eid = $post['eid'];
        }
        $upload->save();

        return success($upload);
    }
    public function uploadList(Request $request)
    {
        $where = [];
        $type = $request->param('type');
        if (isset($type)) {
            $where[] = ['type', '=', $type];
        }
        $list = UploadFile::where($where)
            ->page($request->param('page') ?? 1, $request->param('pageSize') ?? 10)
            ->select();
        return success($list);
    }
    public function qrcode(Request $request)
    {
        $mac = $request->param('mac');
        $client = new Client("dabd78c0de6460190a5baec39a9076a5");
        $result = $client->qrcodeIndex()
            ->withText($mac)
            ->request();
        $base64 = $result["data"]["base64_image"];
        $image = "data:image/jpg;base64," . $base64;
        $imageName = $mac . '.jpg';
        if (strstr($image, ",")) {
            $image = explode(',', $image);
            $image = $image[1];
        }
        $path = "storage/other/qrcode/";
        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        $imageSrc = $path . "/" . $imageName; //图片名字
        //地址存入数据库
        $upload = new UploadFile;
        $upload->url = env('UPLOAD_URL', true) . '/other/qrcode/' . $imageName;
        $upload->filename = $imageName;
        if (isset($post['type'])) {
            $upload->type = $post['type'];
        }
        $upload->save();
        $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
        return success($upload->url);
    }
    public function getSoftwareUrl(Request $request)
    {
        $where = [];
        if ($request->has('type')) {
            $type = $request->param('type');
            $where[] = ['type', '=', $type];
        }
        $softwareurl = DB::table('softwareurl')->where($where)->find();
        return success($softwareurl);
    }
    //获取APP下载内容
    public function getAppUrl(Request $request)
    {
        $where = [];
        if ($request->has('type')) {
            $type = $request->param('type');
            $where[] = ['type', '=', $type];
        }
        if ($request->has('is_show')) {
            $is_show = $request->param('is_show');
            $where[] = ['is_show', '=', $is_show];
        }
        $softwareurl = DB::table('softwareurl')->where($where)->field('id,name,versioncode,versionname,downloadurl,data,is_show,type,update_time')->select();
        return success($softwareurl);
    }
    //修改下载信息
    public function updateAppUrl(Request $request)
    {
        $data = $request->param();
        if($data['id']==2){
            $downloadurl = $data['downloadurl'];
            Db::name('tower_crane')->where('special_customization','=','0')->update(['downloadUrl' => $downloadurl]);
        }
        if($data['id']==6){
            $downloadurl = $data['downloadurl'];
            Db::name('tower_crane')->where('special_customization','=','2')->update(['downloadUrl' => $downloadurl]);
        }
        $id = $request->has('id', 'post');
        $Softwareurl = Softwareurl::find($data['id']);
        foreach ($data as $key => $value) {
            $Softwareurl->$key = $value;
        }
        $rq = $Softwareurl->save();
        if ($rq) {
            return success($data);
        }
    }
}
